package com.herench.bs.transfer;

import com.herench.bs.transfer.Dialect;
import com.herench.bs.utils.DataFormatUtils;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * com.herench.bs.transfer.transfer
 * Created by wenkai on 2017/09/12/17:06
 *
 * @Software: IntelliJ IDEA
 */

public class LoadAllRegistAndAppointRecordsTransfer extends AbstractTransfer {

    public LoadAllRegistAndAppointRecordsTransfer(String method, Client client,String messageMethod) {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String idCard = (String) params.get("idCard");
        String startTime = (String) params.get("startTime");
        String endTime = (String) params.get("endTime");
        Document tplDoc = getTplDoc(getMethod());
        Element dataRowElement = tplDoc.getRootElement().element("CommitData").addElement("Data").addElement("DataRow");
        dataRowElement.addAttribute("QUERY_TYPE", "2").addAttribute("QUERY_CONJ", idCard).addAttribute("QUERY_SOURCE", "0");
        dataRowElement.addAttribute("START_DATE", startTime).addAttribute("END_DATE", endTime);
        return tplDoc.asXML();
    }


    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        List<Map<String, Object>> registAndAppointRecordMaps = new ArrayList<>();
        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            List<Element> appointDetailDataRows = document.getRootElement().element("ReturnData").element("Data").elements();
            if (appointDetailDataRows != null && appointDetailDataRows.size() > 0) {
                for (Element dataRow : appointDetailDataRows) {
                    AppointAndRegistRecordInnerMessage innerMessage = new AppointAndRegistRecordInnerMessage(dataRow);
                    Map<String, Object> recordMap = new HashMap<>(16);
                    String start = "";
                    String end ="";
                    if (innerMessage.getHourStart().equals("08:00")){
                        start = "07:40";
                    }
                    if (innerMessage.getHourStart().equals("09:00")){
                        start = "08:40";
                    }
                    if (innerMessage.getHourStart().equals("10:00")){
                        start = "09:40";
                    }
                    if (innerMessage.getHourStart().equals("13:30")){
                        start = "13:10";
                    }
                    if (innerMessage.getHourStart().equals("14:30")){
                        start = "14:10";
                    }
                    if (innerMessage.getHourEnd().equals("09:00")){
                        end = "08:30";
                    }
                    if (innerMessage.getHourEnd().equals("10:00")){
                        end = "09:30";
                    }
                    if (innerMessage.getHourEnd().equals("11:00")){
                        end = "10:30";
                    }
                    if (innerMessage.getHourEnd().equals("14:30")){
                        end = "14:00";
                    }
                    if (innerMessage.getHourEnd().equals("15:30")){
                        end = "15:00";
                    }
                    recordMap.put("hospitalName",Dialect.getHospitalName());
                    recordMap.put("categorCode", Dialect.getClinicTypeCode(innerMessage.getClinicType()));
                    recordMap.put("categorName", innerMessage.getClinicType());
                    recordMap.put("hisClinicAppointsId", innerMessage.getClinicAppointsId());
                    recordMap.put("appointId",innerMessage.getPlatformAppointId());
                    recordMap.put("ampm", Dialect.getAmpmCode(innerMessage.getVisitTimeDesc()));
                    recordMap.put("ampmName", innerMessage.getVisitTimeDesc());
                    recordMap.put("createDate", innerMessage.getCreateDate());
                    recordMap.put("state", innerMessage.getStatus());
                    recordMap.put("appointDate", innerMessage.getVisitDate());
                    recordMap.put("insuranceType", innerMessage.getInsuranceType());
                    recordMap.put("mtType", innerMessage.getMtType());
                    if (StringUtils.hasLength(innerMessage.getHourStart())) {
                        recordMap.put("period", innerMessage.getHourStart() + "~" + innerMessage.getHourEnd());
                    } else {
                        recordMap.put("period", "");
                    }
                    recordMap.put("clinicLabel", innerMessage.getClinicLabel());
                    recordMap.put("deptId", innerMessage.getVisitDept());
                    recordMap.put("deptName", innerMessage.getVisitDeptName());
                    recordMap.put("docId", innerMessage.getDoctorCode());
                    recordMap.put("doctorName", innerMessage.getDoctor());
                    recordMap.put("name", innerMessage.getName());
                    recordMap.put("idCard", innerMessage.getIdNo());
                    recordMap.put("payType", innerMessage.getPayType());
                    recordMap.put("state", innerMessage.getStatus());
                    recordMap.put("appointmentType", StringUtils.hasLength(innerMessage.getClinicAppointsId()) ? "1" : "2");
                    recordMap.put("phone", innerMessage.getPhoneNumber());
                    recordMap.put("transactNo", innerMessage.getTranNo());
                    recordMap.put("rcptNo", innerMessage.getRcptNo());
                    recordMap.put("insurNo", innerMessage.getInsurNo());
                    recordMap.put("insurVisitId", innerMessage.getInsurVisitId());
                    recordMap.put("insurPay", innerMessage.getInsurPay());
                    recordMap.put("accountPay", innerMessage.getAccountPay());
                    recordMap.put("ownPay", innerMessage.getOwnPay());
                    recordMap.put("patientType", innerMessage.getChargeType());
                    if ("0".equals(innerMessage.getStatus())) {
                        recordMap.put("innerSort", innerMessage.getSerialNo());
                        recordMap.put("queueNum", "0");
                        recordMap.put("regFee", innerMessage.getRegistFee());
                    } else if ("2".equals(innerMessage.getStatus())) {
                        recordMap.put("queueNum", innerMessage.getSerialNo());
                        recordMap.put("innerSort", "0");
                        recordMap.put("regFee", innerMessage.getClinicFee());
                    }
                    recordMap.put("visitNo", innerMessage.getVisitNo());
                    recordMap.put("cardNum", innerMessage.getCardId());
                    recordMap.put("otherFee", innerMessage.getOtherFee());
                    if (innerMessage.getStatus().equals("0")) {
                        recordMap.put("suggestion", "您的报到时间为:" + innerMessage.getVisitDate() + " " + start + "——" + end + "。");
                    }else{
                        recordMap.put("suggestion","");
                    }
                    registAndAppointRecordMaps.add(recordMap);
                }
            }


            //按照“appointDate”降序排序
            Collections.sort(registAndAppointRecordMaps, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Date a = null;
                    Date b = null;
                    try {
                        a = DataFormatUtils.parserDay((String) o1.get("appointDate"));
                        b = DataFormatUtils.parserDay((String) o2.get("appointDate"));
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("LoadAllRegistAndAppointRecordsTransfer>>>toResultMessage>>>compare:appointDate日期格式转换失败！");
                    }
                    int compareRes = a.compareTo(b);
                    return (compareRes < 0 ? 1 : (compareRes < 1 ? 0 : -1));
                }
            });

            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        } else {
            resMap.put("code", "-15");
            resMap.put("message", executeMessage);
        }
        resMap.put("records", registAndAppointRecordMaps);
        return resMap;
    }


    private class AppointAndRegistRecordInnerMessage implements InnerMessage {
        private String clinicLabel;
        private String visitTimeDesc;
        private String patientId;
        private String idNo;
        private String name;
        private String sex;
        private String age;
        private String identity;
        private String chargeType;
        private String clinicType;
        private String visitDept;
        private String doctor;
        private String registFee;
        private String clinicFee;
        private String otherFee;
        private String clinicCharge;
        private String personNo;
        private String visitNo;
        private String visitDate;
        private String rcptNo;
        private String insurNo;
        private String insurVisitId;
        private String insuranceType;
        private String payType;
        private String mtType;
        private String insurPay;
        private String accountPay;
        private String ownPay;
        private String tranNo;
        private String registrationStatus;
        private String deptLocation;
        private String identityName;
        private String chargeTypeName;
        private String visitDeptName;
        private String clinicAppointsId;
        private String ptPrintTimes;
        private String cardId;
        private String serialNo;
        private String status;
        private String registType;
        private String moneyType;
        private String registSource;
        private String doctorCode;
        private String hourStart;
        private String hourEnd;
        private String createDate;
        private String phoneNumber;
        private String platformAppointId;


        public AppointAndRegistRecordInnerMessage(Element element) {
            this.clinicLabel = element.attributeValue("CLINIC_LABEL");
            this.visitTimeDesc = element.attributeValue("VISIT_TIME_DESC");
            this.patientId = element.attributeValue("PATIENT_ID");
            this.idNo = element.attributeValue("ID_NO");
            this.name = element.attributeValue("NAME");
            this.sex = element.attributeValue("SEX");
            this.age = element.attributeValue("AGE");
            this.identity = element.attributeValue("IDENTITY");
            this.chargeType = element.attributeValue("CHARGE_TYPE");
            this.clinicType = element.attributeValue("CLINIC_TYPE");
            this.visitDept = element.attributeValue("VISIT_DEPT");
            this.doctor = element.attributeValue("DOCTOR");
            this.registFee = element.attributeValue("REGIST_FEE");
            this.clinicFee = element.attributeValue("CLINIC_FEE");
            this.otherFee = element.attributeValue("OTHER_FEE");
            this.clinicCharge = element.attributeValue("CLINIC_CHARGE");
            this.personNo = element.attributeValue("PERSON_NO");
            this.visitNo = element.attributeValue("VISIT_NO");
            this.visitDate = element.attributeValue("VISIT_DATE");
            this.rcptNo = element.attributeValue("RCPT_NO");
            this.insurNo = element.attributeValue("INSUR_NO");
            this.insurVisitId = element.attributeValue("INSUR_VISIT_ID");
            this.insuranceType = element.attributeValue("INSURANCE_TYPE");
            this.payType = element.attributeValue("PAY_TYPE");
            this.mtType = element.attributeValue("MT_TYPE");
            this.insurPay = element.attributeValue("INSUR_PAY");
            this.accountPay = element.attributeValue("ACCOUNT_PAY");
            this.ownPay = element.attributeValue("OWN_PAY");
            this.tranNo = element.attributeValue("TRAN_NO");
            this.registrationStatus = element.attributeValue("REGISTRATION_STATUS");
            this.deptLocation = element.attributeValue("DEPT_LOCATION");
            this.identityName = element.attributeValue("IDENTITY_NAME");
            this.chargeTypeName = element.attributeValue("CHARGE_TYPE_NAME");
            this.visitDeptName = element.attributeValue("VISIT_DEPT_NAME");
            this.clinicAppointsId = element.attributeValue("CLINIC_APPOINTS_ID");
            this.ptPrintTimes = element.attributeValue("PT_PRINT_TIMES");
            this.cardId = element.attributeValue("CARD_ID");
            this.serialNo = element.attributeValue("SERIAL_NO");
            this.status = element.attributeValue("STATUS");
            this.registType = element.attributeValue("REGIST_TYPE");
            this.moneyType = element.attributeValue("MONEY_TYPE");
            this.registSource = element.attributeValue("REGIST_SOURCE");
            this.doctorCode = element.attributeValue("DOCTOR_CODE");
            this.hourStart = element.attributeValue("HOUR_START");
            this.hourEnd = element.attributeValue("HOUR_END");
            this.createDate = element.attributeValue("CREATE_DATE");
            this.phoneNumber = element.attributeValue("PHONE_NUMBER");
            this.platformAppointId = element.attributeValue("PLATFORM_APPOINT_ID");
        }

        public String getPlatformAppointId() {
            return platformAppointId;
        }

        public void setPlatformAppointId(String platformAppointId) {
            this.platformAppointId = platformAppointId;
        }

        public String getClinicLabel() {
            return clinicLabel;
        }

        public void setClinicLabel(String clinicLabel) {
            this.clinicLabel = clinicLabel;
        }

        public String getVisitTimeDesc() {
            return visitTimeDesc;
        }

        public void setVisitTimeDesc(String visitTimeDesc) {
            this.visitTimeDesc = visitTimeDesc;
        }

        public String getPatientId() {
            return patientId;
        }

        public void setPatientId(String patientId) {
            this.patientId = patientId;
        }

        public String getIdNo() {
            return idNo;
        }

        public void setIdNo(String idNo) {
            this.idNo = idNo;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public String getAge() {
            return age;
        }

        public void setAge(String age) {
            this.age = age;
        }

        public String getIdentity() {
            return identity;
        }

        public void setIdentity(String identity) {
            this.identity = identity;
        }

        public String getChargeType() {
            return chargeType;
        }

        public void setChargeType(String chargeType) {
            this.chargeType = chargeType;
        }

        public String getClinicType() {
            return clinicType;
        }

        public void setClinicType(String clinicType) {
            this.clinicType = clinicType;
        }

        public String getVisitDept() {
            return visitDept;
        }

        public void setVisitDept(String visitDept) {
            this.visitDept = visitDept;
        }

        public String getDoctor() {
            return doctor;
        }

        public void setDoctor(String doctor) {
            this.doctor = doctor;
        }

        public String getRegistFee() {
            return registFee;
        }

        public void setRegistFee(String registFee) {
            this.registFee = registFee;
        }

        public String getClinicFee() {
            return clinicFee;
        }

        public void setClinicFee(String clinicFee) {
            this.clinicFee = clinicFee;
        }

        public String getOtherFee() {
            return otherFee;
        }

        public void setOtherFee(String otherFee) {
            this.otherFee = otherFee;
        }

        public String getClinicCharge() {
            return clinicCharge;
        }

        public void setClinicCharge(String clinicCharge) {
            this.clinicCharge = clinicCharge;
        }

        public String getPersonNo() {
            return personNo;
        }

        public void setPersonNo(String personNo) {
            this.personNo = personNo;
        }

        public String getVisitNo() {
            return visitNo;
        }

        public void setVisitNo(String visitNo) {
            this.visitNo = visitNo;
        }

        public String getVisitDate() {
            return visitDate;
        }

        public void setVisitDate(String visitDate) {
            this.visitDate = visitDate;
        }

        public String getRcptNo() {
            return rcptNo;
        }

        public void setRcptNo(String rcptNo) {
            this.rcptNo = rcptNo;
        }

        public String getInsurNo() {
            return insurNo;
        }

        public void setInsurNo(String insurNo) {
            this.insurNo = insurNo;
        }

        public String getInsurVisitId() {
            return insurVisitId;
        }

        public void setInsurVisitId(String insurVisitId) {
            this.insurVisitId = insurVisitId;
        }

        public String getInsuranceType() {
            return insuranceType;
        }

        public void setInsuranceType(String insuranceType) {
            this.insuranceType = insuranceType;
        }

        public String getPayType() {
            return payType;
        }

        public void setPayType(String payType) {
            this.payType = payType;
        }

        public String getMtType() {
            return mtType;
        }

        public void setMtType(String mtType) {
            this.mtType = mtType;
        }

        public String getInsurPay() {
            return insurPay;
        }

        public void setInsurPay(String insurPay) {
            this.insurPay = insurPay;
        }

        public String getAccountPay() {
            return accountPay;
        }

        public void setAccountPay(String accountPay) {
            this.accountPay = accountPay;
        }

        public String getOwnPay() {
            return ownPay;
        }

        public void setOwnPay(String ownPay) {
            this.ownPay = ownPay;
        }

        public String getTranNo() {
            return tranNo;
        }

        public void setTranNo(String tranNo) {
            this.tranNo = tranNo;
        }

        public String getRegistrationStatus() {
            return registrationStatus;
        }

        public void setRegistrationStatus(String registrationStatus) {
            this.registrationStatus = registrationStatus;
        }

        public String getDeptLocation() {
            return deptLocation;
        }

        public void setDeptLocation(String deptLocation) {
            this.deptLocation = deptLocation;
        }

        public String getIdentityName() {
            return identityName;
        }

        public void setIdentityName(String identityName) {
            this.identityName = identityName;
        }

        public String getChargeTypeName() {
            return chargeTypeName;
        }

        public void setChargeTypeName(String chargeTypeName) {
            this.chargeTypeName = chargeTypeName;
        }

        public String getVisitDeptName() {
            return visitDeptName;
        }

        public void setVisitDeptName(String visitDeptName) {
            this.visitDeptName = visitDeptName;
        }

        public String getClinicAppointsId() {
            return clinicAppointsId;
        }

        public void setClinicAppointsId(String clinicAppointsId) {
            this.clinicAppointsId = clinicAppointsId;
        }

        public String getPtPrintTimes() {
            return ptPrintTimes;
        }

        public void setPtPrintTimes(String ptPrintTimes) {
            this.ptPrintTimes = ptPrintTimes;
        }

        public String getCardId() {
            return cardId;
        }

        public void setCardId(String cardId) {
            this.cardId = cardId;
        }

        public String getSerialNo() {
            return serialNo;
        }

        public void setSerialNo(String serialNo) {
            this.serialNo = serialNo;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getRegistType() {
            return registType;
        }

        public void setRegistType(String registType) {
            this.registType = registType;
        }

        public String getMoneyType() {
            return moneyType;
        }

        public void setMoneyType(String moneyType) {
            this.moneyType = moneyType;
        }

        public String getRegistSource() {
            return registSource;
        }

        public void setRegistSource(String registSource) {
            this.registSource = registSource;
        }

        public String getDoctorCode() {
            return doctorCode;
        }

        public void setDoctorCode(String doctorCode) {
            this.doctorCode = doctorCode;
        }

        public String getHourStart() {
            return hourStart;
        }

        public void setHourStart(String hourStart) {
            this.hourStart = hourStart;
        }

        public String getHourEnd() {
            return hourEnd;
        }

        public void setHourEnd(String hourEnd) {
            this.hourEnd = hourEnd;
        }

        public String getCreateDate() {
            return createDate;
        }

        public void setCreateDate(String createDate) {
            this.createDate = createDate;
        }

        public String getPhoneNumber() {
            return phoneNumber;
        }

        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }
    }


}
